package com.base.mail.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.base.mail.R;
import com.base.mail.databinding.ItemTreeDepartmentBinding;
import com.base.mail.tree.Node;
import com.base.mail.tree.TreeRecyclerAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangke on 2017-1-14.
 */
public class StructureAdapter extends TreeRecyclerAdapter {

    public static String CHECKID = "";
    public CheckBox box = null;
    private int type_dept= 0;
    private int type_user= 6;

    private List<Node> mData;

    public StructureAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand) {
        super(mTree, context, datas, defaultExpandLevel, iconExpand, iconNoExpand);
        this.mData = datas;
    }

    public StructureAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel) {
        super(mTree, context, datas, defaultExpandLevel);
        this.mData = datas;
    }


    MyHoder holder=null;
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        if(type_user == viewType){
//            holder = new UserHolder(View.inflate(mContext, R.layout.item_tree_user, null));
//        }else{
//        if (holder == null) {
//            holder = ;
//        }
        return new MyHoder(View.inflate(mContext, R.layout.item_tree_department,null));
    }

    @Override
    public void onBindViewHolder(final Node node, final RecyclerView.ViewHolder holder, int position) {
//       if (holder instanceof MyHoder){
        ((MyHoder) holder).mBinding.setEntity(node);
        if (node.getState().equals(type_user+"")){
            ((MyHoder) holder).mBinding.depart.setVisibility(View.GONE);
            ((MyHoder) holder).mBinding.user.setVisibility(View.VISIBLE);
        }else {
            ((MyHoder) holder).mBinding.depart.setVisibility(View.VISIBLE);
            ((MyHoder) holder).mBinding.user.setVisibility(View.GONE);
        }

        if ("2".equals(node.getState())){
            ((MyHoder) holder).mBinding.check.setVisibility(View.INVISIBLE);
        }else {
            ((MyHoder) holder).mBinding.check.setVisibility(View.VISIBLE);
        }
        ((MyHoder) holder).mBinding.check.setChecked(node.isChecked());
        ((MyHoder) holder).mBinding.check.setTag(node.getId());
        ((MyHoder) holder).mBinding.check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (box!=null)box.setChecked(false);
                if (((MyHoder) holder).mBinding.check.isChecked()){
                    box = ((MyHoder) holder).mBinding.check;
                    CHECKID = node.getId().toString();
                    setcb(false);
                    node.setChecked(true);
                    notifyDataSetChanged();
                }else {
                    node.setChecked(false);
                    if (box!=null){
                        if (box.getTag().toString().equals(((MyHoder) holder).mBinding.check.getTag().toString())){
                            CHECKID = "";
                        }else {
                            box.setChecked(false);
                        }
                    }
                }
            }
        });

//       }else if (holder instanceof UserHolder){
//           ((UserHolder) holder).mBinding.setEntity(node);
//       }
    }
    private void setcb(boolean isflag){
        for (Node node : mData){
            node.setChecked(isflag);
        }
    }

    class MyHoder extends RecyclerView.ViewHolder {

        ItemTreeDepartmentBinding mBinding;
        public MyHoder(View itemView) {
            super(itemView);
            mBinding = DataBindingUtil.bind(itemView);
        }

    }

}
